package com.ryan.sbnote.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class NoteWriteView extends View {
	private static final String TAG = "NoteWriteView";
	private Paint mPaint;
	private Canvas mCacheCanvas;
	private Bitmap mCachebBitmap;
	private Path mPath;
	private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    private Paint   mBitmapPaint;
    private OnWriteChangedListener mListener;
	
	public NoteWriteView(Context context) {
		super(context);
	}
	public NoteWriteView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	public NoteWriteView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setOnWriteChangedListener(OnWriteChangedListener listener){
		this.mListener = listener;
	}
	
	public void invokeChange(){
		
//		String tmpFile = getContext().getCacheDir()+"/tmp_0";
//		
//		
//		try {
//			FileOutputStream out = new FileOutputStream( tmpFile);
//			mCachebBitmap.compress(CompressFormat.PNG, 100, out);
//			out.flush();
//			out.close();
//		} catch (IOException e) {
//			LogUtil.e(TAG,"E",e);
//		}
//		
//		Options options = new Options();
//		options.inJustDecodeBounds = true;
//		BitmapFactory.decodeFile(tmpFile,options);
//		options.inJustDecodeBounds = false;
//		options.inSampleSize = options.outWidth/50;
//		options.inDither=true;
//		options.inPreferredConfig = Config.ARGB_8888;
//		Bitmap scaledBitmap = BitmapFactory.decodeFile(tmpFile, options);
//		Bitmap.createBitmap(mCachebBitmap, 0, 0, mCachebBitmap.getWidth(), mCachebBitmap.getHeight(), new Matrix(), true);
		
		
		Bitmap b2 = Bitmap.createScaledBitmap(mCachebBitmap, 70, 70, true);
		
		
//		try {
//			FileOutputStream out = new FileOutputStream( tmpFile+"_2");
//			b2.compress(CompressFormat.PNG, 100, out);
//			out.flush();
//			out.close();
//		} catch (IOException e) {
//			LogUtil.e(TAG,"E",e);
//		}
//		
//		try {
//			FileOutputStream out = new FileOutputStream( tmpFile+"_1");
//			scaledBitmap.compress(CompressFormat.PNG, 100, out);
//			out.flush();
//			out.close();
//		} catch (IOException e) {
//			LogUtil.e(TAG,"E",e);
//		}
		
		mListener.writeChanged(b2);
		mCachebBitmap.eraseColor(0);
		invalidate();
	}
	
	public void init(int width,int height){
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setStrokeWidth(10);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(Color.RED);
		mPath = new Path();
		mBitmapPaint = new Paint(Paint.DITHER_FLAG);
		mCachebBitmap = Bitmap.createBitmap(width,height, Config.ARGB_8888);
		mCacheCanvas = new Canvas(mCachebBitmap);
	}


	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.GRAY);
		canvas.drawBitmap(mCachebBitmap, 0, 0, mBitmapPaint);
		canvas.drawPath(mPath, mPaint);
	}

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        	mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }
    private void touch_up() {
    	mPath.lineTo(mX, mY);
    	mCacheCanvas.drawPath(mPath, mPaint);
        mPath.reset();
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                removeCallbacks(writeCallback);
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                postDelayed(writeCallback, 700);
                break;
        }
        return true;
    }
    
    private Runnable writeCallback = new Runnable() {
		@Override
		public void run() {
			invokeChange();
		}
	};
    
    public static interface OnWriteChangedListener{
    	public void writeChanged(Bitmap bitmap);
    }
}
